package finedev.android.venue;

import java.io.IOException;
import java.util.HashMap;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
import com.google.android.gcm.GCMRegistrar;

public class IntroActivity extends NetworkActivity implements OnClickListener {
	
	private Activity mActivity;
	
	private final String TAG = "intro";
	private final String SENDER_ID = "";
	
	public static HashMap<String, String> mCheckResult = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intro);
        
        GCMRegistrar.checkDevice( this );
        GCMRegistrar.checkManifest( this );
        final String regId = GCMRegistrar.getRegistrationId( this );
        
        if ( regId.equals("") ){
        	GCMRegistrar.register(this, SENDER_ID);
        } else {
        	Log.d(TAG, "already registered" );
        }
        
        mActivity = this;
        
        if ( BasicInfo.FacebookInstance == null ) {
    		BasicInfo.FacebookInstance = new Facebook( BasicInfo.APP_ID );
    		BasicInfo.FacebookRunnerInstance = new AsyncFacebookRunner( BasicInfo.FacebookInstance );
    	}
        
        Button btn_facebook = (Button) findViewById(R.id.intro_button_facebook);
        Button btn_login = (Button) findViewById(R.id.intro_button_login);
        
        btn_facebook.setOnClickListener( this );
        btn_login.setOnClickListener( this );
        
    }
	public void onClick(View view) {
		if ( view.getId() == R.id.intro_button_facebook ) {
			BasicInfo.FacebookLogin( TAG, mActivity);
		} else if ( view.getId() == R.id.intro_button_login ) {
			startActivity( new Intent( getApplicationContext(), LoginActivity.class) );
		}
		
	}
	
	@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        if ( requestCode == BasicInfo.FACEBOOK_REQUEST_CODE )
        	BasicInfo.FacebookInstance.authorizeCallback(requestCode, resultCode, data);
        
    }
	
	@Override
	public void ReceiveSuccess(String str) {
		Log.i(TAG, "ReceiveSuccess(String str) : " + str );
		try {
			mCheckResult = new ObjectMapper().readValue( str, HashMap.class );
			if ( String.valueOf( mCheckResult.get("result") ).equals("true") ) {
				BasicInfo.USER_KEY = String.valueOf( mCheckResult.get("user_key") );
				BasicInfo.USER_LEVEL = String.valueOf( mCheckResult.get("access_lv") );
				BasicInfo.USER_INCOME = String.valueOf( mCheckResult.get("income") );
				startActivity( new Intent( getApplicationContext(), MainTabActivity.class) );
			} else {
				startActivity( new Intent( getApplicationContext(), JoinActivity.class) );
			}
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void ReceiveFail(String str) {
		Log.i(TAG, "ReceiveFail()" );
	}
	
}

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.activity_intro, menu);
//        return true;
//    }